
//
//  DatasourceViewController.m
//  ebook
//
//  Created by 吴超飞 on 16/1/15.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "DatasourceViewController.h"
#import "ReaderGlobalModel.h"

@interface DatasourceViewController ()

@property (nonatomic, strong) UIViewController *currentViewController;

@end

@implementation DatasourceViewController

- (instancetype)init {
    
    self = [super init];
    if (self) {
        _pageDatas = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)updateDatasource:(NSDictionary *)dict{
 
    NSArray *dataArray = [dict objectForKey:@"data"];
    if(dataArray&&dataArray.count>0){
        _pageDatas = [NSArray arrayWithArray:dataArray];
        _index = [[dict objectForKey:@"index"] integerValue];
    }else{
        MTLog(@"获取pageDatas失败");
    }
}

- (DataViewController *)viewControllerAtIndex:(NSUInteger)index{
    
    if (([_pageDatas count] == 0) || (index >= [_pageDatas count])) {
        return nil;
    }
    PageData *data = _pageDatas[index];
    _currentPageData = data;
    
    DataViewController *dataViewController = [[DataViewController alloc] init];
    dataViewController.dataObject = data.text;
    dataViewController.chapter = data.chapter;
    dataViewController.currentPage = data.currentPage;
    dataViewController.totalPage = data.totalPage;
    
    if(!dataViewController.dataObject||!dataViewController.chapter.chaptername)
        return nil;//防止出现空白页
    
    return dataViewController;
}

- (NSUInteger)indexOfViewController:(DataViewController *)viewController{
    
    NSInteger index=0;
    if(_pageDatas.count>0)
        for (int i=0; i<_pageDatas.count; i++) {
            PageData *obj = _pageDatas[i];
            if([obj.chapter.chapterid integerValue]==[viewController.chapter.chapterid integerValue]&&obj.currentPage==viewController.currentPage){
                index = i;
                break;
            }
        }
    
    return index;
}

#pragma mark - UIPageViewController DataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    
    if([ReaderGlobalModel shareModel].readerConfig.PageTransitionStyle==UIPageViewControllerTransitionStylePageCurl&&_pageIsAnimating){
        MTLog(@"pageIsAnimating");
        return nil;
    }
    DataViewController *vc = nil;
    
    if ([ReaderGlobalModel shareModel].readerConfig.PageTurnStyle==PageTurnStyle_Simulat&&[viewController isKindOfClass:[DataViewController class]]) {//仿真背面
        
        self.currentViewController = viewController;
        BackViewController *backViewController = [BackViewController new];
        [backViewController updateWithViewController:(id)viewController];
        return backViewController;
    }
    
    //仿真正面、其他翻页方式
    
    NSUInteger index = [self indexOfViewController:(DataViewController *)([ReaderGlobalModel shareModel].readerConfig.PageTurnStyle==PageTurnStyle_Simulat?_currentViewController:viewController)];
    
    if(index == NSNotFound||index == 0){
        vc = nil;
    }else{
        vc = [self viewControllerAtIndex:--index];
    }
    BOOL shouldGet = index==5;//后退翻到第五页了需要更新数据
    
    if ([self.delegate respondsToSelector:@selector(datasourceViewController:isForward:shouldGetData:currentViewcontroller:)]) {
        [self.delegate datasourceViewController:self isForward:NO shouldGetData:shouldGet currentViewcontroller:vc];
    }
    
    //无动画时不要做翻页
    if([ReaderGlobalModel shareModel].readerConfig.IsPageTransitionNoAnimate)
        vc=nil;
    
    return vc;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    
    if([ReaderGlobalModel shareModel].readerConfig.PageTransitionStyle==UIPageViewControllerTransitionStylePageCurl&&_pageIsAnimating){
        MTLog(@"pageIsAnimating");
        return nil;
    }
    DataViewController *vc = nil;
    
    if ([ReaderGlobalModel shareModel].readerConfig.PageTurnStyle==PageTurnStyle_Simulat&&[viewController isKindOfClass:[DataViewController class]]) {//仿真背面
        
        self.currentViewController = viewController;
        BackViewController *backViewController = [BackViewController new];
        [backViewController updateWithViewController:(id)viewController];
        return backViewController;
        
    }else{//仿真正面、其他翻页方式
        
        NSUInteger index = [self indexOfViewController:(DataViewController *)([ReaderGlobalModel shareModel].readerConfig.PageTurnStyle==PageTurnStyle_Simulat?_currentViewController:viewController)];
        
        if (index == NSNotFound||index==_pageDatas.count-1) {
            vc = nil;
        }else{
            vc = [self viewControllerAtIndex:++index];
        }
        BOOL shouldGet = index==_pageDatas.count-5;//到倒数第五页了需要更新数据
        
        if ([self.delegate respondsToSelector:@selector(datasourceViewController:isForward:shouldGetData:currentViewcontroller:)]) {
            [self.delegate datasourceViewController:self isForward:YES shouldGetData:shouldGet currentViewcontroller:vc];
        }
        
        //无动画时不要做翻页
        if([ReaderGlobalModel shareModel].readerConfig.IsPageTransitionNoAnimate)
            vc = nil;
    }
    
    return vc;
}

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers {
    
    self.pageIsAnimating = YES;
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    
    if(finished){
        self.pageIsAnimating = NO;
    }
}

- (void)onStopInteroperate {
    self.pageIsAnimating = NO;
}

@end
